# !/usr/bin/env python
# encoding: utf-8


"""
  @author: gaogao
  @file: oo_te_alignment.py
  @time: 2022/3/13 14:01
  @desc:稳偏的脚本
"""
import time

from ATS.application.LASER_App.N7744C_App import N7744C_App
from ATS.application.LASER_App.L8164B_App import L8164B_App
from ATS.application.LASER_App.N7786C_App import N7786C_App
from utils.log_util import loggings
try:
    from ATS.switchdriver.LightSwitchDriver import LightSwitchDriver
except Exception as e:
    raise ValueError("光开关不在线，请重启光开关！")


def oo_te_alignment(para, max_min=1):
    try:
        inst_light = LightSwitchDriver.instance()

        if not inst_light.is_connected:
            inst_light.connect()
        inst_light.set_all_switch("01", "05")
        time.sleep(1)
        loggings.info("switch" + str(inst_light.query_switch()))

        # max_min: 0代表显示最小,1代表显示最大
        global polarization_data

        # print("n7744c_app",n7744c_app)
        n7786c_app = N7786C_App("SS1")
        n7744c_app = N7744C_App(para.n7744_setting["title"])
        l8164b_app = L8164B_App(para.l8164_setting["title"])
        n7744c_app.set_logging_state("STOP")
        # l8164b_app.cls()
        l8164b_app.set_wave_length(para.l8164_setting["wave_length"][0])
        l8164b_app.set_power_unit()
        l8164b_app.set_power_value(para.l8164_setting["power_value"])
        time.sleep(int(para.l8164_setting["stable_time"]))
        l8164b_app.output_on()
        n7744c_app.reset()
        n7744c_app.set_power_range(para.n7744_setting["power_range"])
        # print("power_range_after")
        n7744c_app.set_auto_gain()
        # print("set_auto_gain")
        n7744c_app.trigger_input_mode()
        n7744c_app.set_power_wave_length(para.l8164_setting["wave_length"][0])
        n7744c_app.set_logging_points()

        # init
        n7786c_app.reset()
        n7786c_app.set_stabilization_mode(0)
        n7786c_app.set_random_points_stepwidth()
        n7786c_app.set_trigger_conf_mode(4)
        n7786c_app.set_sample_point_rate()

        n7786c_app.set_wave_length(para.l8164_setting["wave_length"][0])
        n7786c_app.start_polarimeter_logging()
        n7786c_app.start_polarization_sequence()
        n7744c_app.set_logging_state("START")
        time.sleep(3)
        n7786c_app.trigger()
        # n7744c_app.tigger_status()
        while True:
            state = n7744c_app.get_func_state()
            if state == "LOGGING_STABILITY,COMPLETE":
                result_list = n7744c_app.get_func_res()
                # print("result", result_list)
                break
        # print(max(result_list))
        max_index = result_list.index(max(result_list))
        min_index = result_list.index(min(result_list))
        while True:
            log_value = n7786c_app.get_logged_value()
            if log_value == "+0":
                polarization_data = n7786c_app.get_polarization_sweep_data()
                break
        n7786c_app.stop_logging_polcontroller()
        state = n7786c_app.get_opc_state()
        if state == "1":
            n7786c_app.set_external_trigger_delay()
            n7786c_app.set_trigger_conf_mode()
            n7786c_app.set_stabilization_mode(1)

        max_polar = [polarization_data[max_index * 3], polarization_data[max_index * 3 + 1],
                     polarization_data[max_index * 3 + 2]]
        min_polar = [polarization_data[min_index * 3], polarization_data[min_index * 3 + 1],
                     polarization_data[min_index * 3 + 2]]
        # print(max_polar)
        if max_min:
            n7786c_app.set_polarization_value(*max_polar)
        else:
            n7786c_app.set_polarization_value(*min_polar)
    except Exception as e:
        loggings.error("oo_te_alignment" + str(e))



oo_te_alignment(locals()["para"])
